<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <title> 添加暂存代码 </title>
        <script language="Javascript" type="text/javascript" src="../../edit_area/edit_area_full.js"></script>
    </head>
    <body>
    <?php require("front_view_nav.php"); ?>
    <div class="col-lg-offset-1 col-lg-10 col-sm-12 col-xs-12">
        <div id="stash_code_info" class="panel panel-default" style="margin-left: -1.3rem; margin-right: -1.3rem;">
            <div class="panel-body">
                <form class = "form-horizontal" @submit.prevent="postStashCode()">
                    <div class="form-group">
                        <label class="col-sm-offset-1 col-sm-1 control-label">题目ID:</label>
                        <div class="col-sm-2">
                            <input v-model="codeInfo.problem_id" class="form-control" placeholder="题目ID" type="text">
                        </div>
                        <label class="col-sm-1 control-label">标题:</label>
                        <div class="col-sm-4">
                            <input v-model="codeInfo.title" class="form-control" placeholder="标题" type="text">
                        </div>
                        <div class="col-sm-2 control-label" style="color: blue;">建议添加上题目ID</div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12 col-xs-12 col-lg-12">
                            <textarea v-model="codeInfo.source"
                                      class="form-control"
                                      style="min-height: 60rem;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-4 col-sm-1 col-lg-1 col-xs-4">
                            <button type="submit" class="form-control btn btn-primary" >提交</button>
                        </div>
                        <div class="col-sm-1 col-lg-1 col-xs-4">
                            <button @click="returnBack()" type="button" class="form-control btn btn-primary" >返回</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script>
        var app = new Vue({
            el: "#stash_code_info",
            data:{
                codeInfo: {
                    id:"",
                    problem_id:"",
                    title:"",
                    source:"",
                }
            },

            mounted:function () {
                // 修改无法返回的bug，此种方法，貌似无效
                let src = getUrlKey('source');
                if (src != null && src.length > 0 ) {
                    window.location.href="stash_code_list.php";
                    return;
                }
                this.loadCodeInfo();
            },

            methods:{

                // 加载cookie中的用户信息
                loadCodeInfo:function() {
                    this.codeInfo.id = getUrlKey('id');
                    if ( ! isNull(this.codeInfo.id) ) {
                        this.getCodeDetail();
                    }
                },
                
                getCodeDetail:function () {
                    let reqData = {};
                    reqData.id = this.codeInfo.id;
                    reqData.op = "fetch";
                    let param = "para=" + JSON.stringify(reqData);
                    new Promise(function (resolve, reject) {
                        $.ajax({
                            type: 'post',
                            url: '../model/stash_code_router.php',
                            data: param,
                            dataType: 'json',
                            success: function (retData) {
                                if (retData.status) {
                                    resolve(retData.dat);
                                }
                                else {
                                    reject(retData.msg);
                                }
                            },
                            error:function (dat) {
                                //alert("dddeeee");
                            },
                        });
                    }).then(function (detail) {
                        app.codeInfo.id = detail.id;
                        app.codeInfo.problem_id = detail.problem_id;
                        app.codeInfo.title = detail.title;
                        app.codeInfo.source = Base64.decode(detail.source);
                    }).catch(function (errMsg) {
                        alert('获取代码信息失败:'+errMsg);
                    });
                },

                postStashCode:function(id) {
                    if ( this.codeInfo.title == "" ) {
                        alert('标题不能为空');
                        return;
                    }
                    if (  this.codeInfo.source.length < 5 ) {
                        alert('代码长度太小');
                        return;
                    }
                    if ( this.codeInfo.id != null && this.codeInfo.id.length > 2 ) {
                        this.editCode();
                    }
                    else {
                        this.addCode();
                    }
                },

                editCode:function () {
                    let reqData = {};
                    reqData.id = this.codeInfo.id;
                    reqData.problem_id = this.codeInfo.problem_id
                    // 修改bug:标题中有 & 会造成提交错误
                    reqData.title = encodeURIComponent(this.codeInfo.title)

                    // 去除末尾多余的换行符
                    this.codeInfo.source = this.codeInfo.source.replace(/^\s+|\s+$/g,'');
                    // base64加密
                    let base64Str = Base64.encode(this.codeInfo.source);
                    // url编码，避免 +、=在传输过程中的干扰
                    reqData.source = encodeURIComponent(base64Str);
                    reqData.op = "edit";
                    let param = "para="+JSON.stringify(reqData);
                    new Promise(function(resolve, reject) {
                        $.ajax({
                            type:'post',
                            url:'../model/stash_code_router.php',
                            data:param,
                            dataType:'json',
                            success:function (retData) {
                                if ( retData.status ) {
                                    resolve();
                                }
                                else {
                                    reject(retData.msg);
                                }
                            }
                        });
                    }).then(function() {
                        window.location.href="stash_code_list.php";
                    }).catch(function(errMsg) {
                        alert('修改代码失败:'+errMsg);
                    });
                },

                addCode:function () {
                    let reqData = {};
                    reqData.problem_id = this.codeInfo.problem_id;
                    reqData.title = encodeURIComponent(this.codeInfo.title)

                    // 去除末尾多余的换行符
                    this.codeInfo.source = this.codeInfo.source.replace(/^\s+|\s+$/g,'');
                    // base64加密
                    let base64Str = Base64.encode(this.codeInfo.source);
                    // url编码，避免 +、=在传输过程中的干扰
                    reqData.source = encodeURIComponent(base64Str);
                    reqData.op = "add";
                    let param = "para="+JSON.stringify(reqData);
                    new Promise(function(resolve, reject) {
                        $.ajax({
                            type:'post',
                            url:'../model/stash_code_router.php',
                            data:param,
                            dataType:'json',
                            success:function (retData) {
                                if ( retData.status ) {
                                    resolve();
                                }
                                else {
                                    reject(retData.msg);
                                }
                            }
                        });
                    }).then(function() {
                        history.go(-1);
                    }).catch(function(errMsg) {
                        alert('添加代码失败:'+errMsg);
                    });
                },

                returnBack:function () {
                    history.go(-1);
                },
            }
        });
    </script>
    </body>
    </html>
<?php
//require("../oj-footer.php");
?>